package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.util.JSONUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONArray;

@Service
public class SewageDetailService {
	/**
	 * 污水日常巡检选项获取
	 */
	@SuppressWarnings("unused")
	public String getSewageRcxjDetail(String tableName, String taskCode) {
		if (tableName.isEmpty() || taskCode.isEmpty()) {
			return "";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String fatherSql = "select S_DOMAINCODE \"id\",S_DOMAINCAPTION \"name\" from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='2' and S_DOMAINCODE like 'A1%' ORDER BY S_DOMAINCODE";
		// 1.获得一级目录
		List<RowBase> fatherList = DataUtil.getSelectRows(fatherSql, parms);

		// 响应结果
		List<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();
		if (null != fatherSql) {
			String id;
			String name;
			String childsql = "select t1.S_DOMAINCAPTION as name, t.item,t.ITEM_VALUE as value from " + tableName
					+ " t left join  (select S_DOMAINCODE,S_DOMAINCAPTION  from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='3')t1  "
					+ " on t.item = t1.S_DOMAINCODE  where t.task_code = '" + taskCode + "'";
			HashMap<String, Object> father;
			for (RowBase bean : fatherList) {
				String sql = null;
				id = bean.get("id").toString();
				name = bean.get("name").toString();
				sql = childsql + "and t.item like '" + id + "%' ORDER BY t.item desc";
				List<RowBase> childList = DataUtil.getSelectRows(sql, parms);
				father = new HashMap<String, Object>();
				father.put("id", id);
				father.put("name", name);
				father.put("childList", childList);
				resultList.add(father);
			}
			JSONArray jsonArray = JSONArray.fromObject(resultList);
			return jsonArray.toString();
		} else {
			return new JSONArray().toString();
		}
	}
	/**
	 * 污水定期维护选项获取
	 */
	@SuppressWarnings("unused")
	public String getSewageDqwhDetail(String tableName, String taskCode) {
		if (tableName.isEmpty() || taskCode.isEmpty()) {
			return "";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String fatherSql = "select S_DOMAINCODE \"id\",S_DOMAINCAPTION \"name\" from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='2' and S_DOMAINCODE like 'A2%' ORDER BY S_DOMAINCODE";
		// 1.获得一级目录
		List<RowBase> fatherList = DataUtil.getSelectRows(fatherSql, parms);

		// 响应结果
		List<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();
		if (null != fatherSql) {
			String id;
			String name;
			String childsql = "select t1.S_DOMAINCAPTION as name, t.item,t.ITEM_VALUE as value from " + tableName
					+ " t left join  (select S_DOMAINCODE,S_DOMAINCAPTION  from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='4')t1  "
					+ " on t.item = t1.S_DOMAINCODE  where t.task_code = '" + taskCode
					+ "' and t1.S_DOMAINCAPTION is not null ";
			HashMap<String, Object> father;
			for (RowBase bean : fatherList) {
				String sql = null;
				id = bean.get("id").toString();
				name = bean.get("name").toString();
				sql = childsql + "and t.item like '" + id + "%' ORDER BY t.item desc";
				List<RowBase> childList = DataUtil.getSelectRows(sql, parms);
				father = new HashMap<String, Object>();
				father.put("id", id);
				father.put("name", name);
				father.put("childList", childList);
				resultList.add(father);
			}
			JSONArray jsonArray = JSONArray.fromObject(resultList);
			return jsonArray.toString();
		} else {
			return new JSONArray().toString();
		}
	}
	/**
	 * android污水水样比对任务详情
	 * 
	 */
	public String getSewageSybdDetail(String tableName, String taskCode) {
		if (tableName.isEmpty() || taskCode.isEmpty()) {
			return "[]";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t.* from " + tableName + " t  where t.task_id = '" + taskCode + "'";
		// parms.add(new GlobalParameter("tableName", tableName));
		// parms.add(new GlobalParameter("taskCode", taskCode));
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() >= 0) {
			return JSONUtils.toJSONString(rows);
		} else {
			return "[]";
		}

	}
	/**
	 * android污水标样核查任务详情
	 * 
	 */
	public String getSewageByhcDetail(String tableName, String taskCode, String stationId) {
		if (tableName.isEmpty() || taskCode.isEmpty() || stationId.isEmpty()) {
			return "[]";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t1.name,t.* from " + tableName
				+ " t left join (select * from BC_SITE_DEVICE_RPT where monitor_point = '" + stationId
				+ "') t1 on t1.monitor_pollutants = t.test_project  where t.task_code = '" + taskCode + "'";
		// parms.add(new GlobalParameter("tableName", tableName));
		// parms.add(new GlobalParameter("taskCode", taskCode));
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() >= 0) {
			return JSONUtils.toJSONString(rows);
		} else {
			return "[]";
		}

	}
	/**
	 * android污水超标比对任务详情
	 * 
	 */
	public String getSewageCbbdDetail(String tableName, String taskCode) {
		if (tableName.isEmpty() || taskCode.isEmpty()) {
			return "[]";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select * from " + tableName + " where task_id = '" + taskCode + "'";
		// parms.add(new GlobalParameter("tableName", tableName));
		// parms.add(new GlobalParameter("taskCode", taskCode));
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() >= 0) {
			return JSONUtils.toJSONString(rows);
		} else {
			return "[]";
		}

	}
	/**
	 * android污水日常校准任务详情
	 * 
	 */
	public String getSewageRcjzDetail(String tableName, String taskCode, String stationId) {
		if (tableName.isEmpty() || taskCode.isEmpty() || stationId.isEmpty()) {
			return "[]";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t1.name as devicename,to_char(t.verify_date,'yyyy-MM-dd HH24:MI:SS') as myverify_date,t.* from OPERATION_DAILY_VERIFICATION t left join (select * from BC_SITE_DEVICE_RPT where monitor_point = '"
				+ stationId + "') t1 on t1.device_typenum = t.device_num where task_code = '" + taskCode + "'";
		// parms.add(new GlobalParameter("tableName", tableName));
		// parms.add(new GlobalParameter("taskCode", taskCode));
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() >= 0) {
			return JSONUtils.toJSONString(rows);
		} else {
			return "[]";
		}

	}
	/**
	 * android污水设备故障维修任务详情
	 * 
	 */
	public String getSewageSbgzwxDetail(String tableName, String taskCode) {
		if (tableName.isEmpty() || taskCode.isEmpty()) {
			return "[]";
		}
		tableName = tableName.toUpperCase();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t1.c_userealname,t.* from " + tableName
				+ " t left join safe_user t1 on t.repair_person = t1.s_userid where t.task_id = '" + taskCode + "'";
		// parms.add(new GlobalParameter("tableName", tableName));
		// parms.add(new GlobalParameter("taskCode", taskCode));
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() >= 0) {
			return JSONUtils.toJSONString(rows);
		} else {
			return "[]";
		}

	}
}
